<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="姓名" prop="name">
				<u-input :border="border" placeholder="请输入姓名" v-model="model.name" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="性别" prop="sex_lable">
				<u-input :border="border" type="select" :select-open="actionSheetShow" v-model="model.sex_lable" placeholder="请选择性别" @click="actionSheetShow = true"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle"  :label-position="labelPosition" label="身份证号" prop="id_card">
				<u-input :border="border" placeholder="请输入身份证号" v-model="model.id_card" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="现居住地址" prop="living">
				<u-input type="textarea" :border="border" placeholder="请填写现居住地址" v-model="model.living" :height="150" />
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="户籍所在地" prop="domicile">
				<u-input :border="border" type="select" v-model="model.domicile" placeholder="请选择户籍所在地" @click="domicileShow = true" />
			</u-form-item>
			<u-form-item :label-style="labelStyle"  :label-position="labelPosition" label="工作单位" prop="employer">
				<u-input :border="border" placeholder="请输入工作单位" v-model="model.employer" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="出发地、途径地及时间" prop="buy_way">
				<u-input type="textarea" :border="border" :placeholder="buy_way_placeholder" v-model="model.buy_way" :height="200" />
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="到沙时间" prop="arrival_time">
				<u-input :border="border" type="select" v-model="model.arrival_time" placeholder="请选择到沙时间" @click="arrivalTimeShow = true"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="出发地" prop="departure">
				<u-input :border="border" type="select" v-model="model.departure" placeholder="请选择出发地" @click="departureShow = true" />
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="最近一次核酸检测时间" prop="check_time">
				<u-input :border="border" type="select" :select-open="checkTimeShow" v-model="model.check_time" placeholder="请选择到沙时间" @click="checkTimeShow = true"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="最近一次核酸检测结果" prop="check_result">
				<u-radio-group v-model="radio" :width="radioCheckWidth" :wrap="radioCheckWrap">
					<u-radio shape="circle" v-for="(item, index) in radioList" :key="index" :name="item.name" @change="radioChange(index)">{{ item.name }}</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="居住地所属社区" prop="community_lable">
				<u-input :border="border" type="select" :select-open="selectShow" v-model="model.community_lable" placeholder="请选择居住地所属社区" @click="selectShow = true"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="上传健康码" prop="image1">
				<u-upload ref="uUpload1" :action="upload_url" width="160" height="160" @on-change="uploadChange1" :multiple="false" :max-count="1"></u-upload>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="上传行程码" prop="image2">
				<u-upload ref="uUpload2" :action="upload_url" width="160" height="160" @on-change="uploadChange2" :multiple="false" :max-count="1"></u-upload>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="手机号码" prop="mobile">
				<u-input :border="border" placeholder="请输入手机号" v-model="model.mobile" type="number"></u-input>
			</u-form-item>
			<u-form-item :label-style="labelStyle" :label-position="labelPosition" label="验证码" prop="code">
				<u-input :border="border" placeholder="请输入验证码" v-model="model.code" type="text"></u-input>
				<u-button slot="right" type="success" size="mini" @click="getCode">{{codeTips}}</u-button>
			</u-form-item>
		</u-form>
		<u-button :throttle-time="2100" @click="submit" type="success" class="u-margin-top-80 u-margin-bottom-80">提交</u-button>
		
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-select mode="single-column" :default-value="selectDefault" :list="selectList" v-model="selectShow" @confirm="selectConfirm"></u-select>
		<u-picker :area-code="domicile_area_code" title="户籍所在地" mode="region" v-model="domicileShow" @confirm="domicileConfirm"></u-picker>
		<u-picker :area-code="departure_area_code" title="出发地" mode="region" v-model="departureShow" @confirm="departureConfirm"></u-picker>
		<u-picker :params="params" title="到沙时间" :start-year="2018" :end-year="2030" mode="time" v-model="arrivalTimeShow" @confirm="arrivalTimeConfirm"></u-picker>
		<u-picker :params="params" title="最近一次核酸检测时间" :start-year="2018" :end-year="2030" mode="time" v-model="checkTimeShow" @confirm="checkTimeConfirm"></u-picker>
		<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		let that = this;
		return {
			model: {
				name: '',
				sex: '',
				id_card: '',
				living: '',
				domicile: '',
				employer: '',
				buy_way: '',
				arrival_time: '',
				departure: '',
				check_time: '',
				check_result: 0,
				community: '',
				image1: '',
				image2: '',
				mobile: '',
				code: '',
				community_lable: '',
			},
			radio: '阴性',
			border: true,
			check: false,
			actionSheetShow: false,
			arrivalTimeShow: false,
			checkTimeShow: false,
			selectShow: false,
			departureShow: false,
			domicileShow: false,
			radioCheckWidth: 'auto',
			radioCheckWrap: false,
			labelPosition: 'top',
			labelStyle: {
				fontWeight: 'bold',
			},
			codeTips: '',
			errorType: ['message','border'],
			buy_way_placeholder: '例：2021年8月14日19点58分从四川省成都市乘坐火车（车次：G25067车厢4F出发）于2021年8月14日24:00到达，而后乘坐私家车到达沙雅县X小区X号楼X单元X楼X户',
			upload_url: this.$u.http.config.baseUrl + '/api/common/upload',
			upload_lists1: [],
			upload_lists2: [],
			params: {
				year: true,
				month: true,
				day: true,
				hour: true,
				minute: true
			},
			actionSheetList: [
				{
					text: '男'
				},
				{
					text: '女'
				},
			],
			selectList: [],
			selectDefault: [0],
			radioList: [
				{
					name: '阴性',
					checked: true,
				},
				{
					name: '阳性',
					checked: false,
				},
			],
			domicile_code: {
				province: '',
				city: '',
				area: '',
			},
			departure_code: {
				province: '',
				city: '',
				area: '',
			},
			rules: {
				name: [
					{
						required: true,
						message: '请输入姓名',
						trigger: 'blur' ,
					},
					{
						min: 2,
						max: 5,
						message: '姓名长度在2到5个字符',
						trigger: ['change','blur'],
					},
					{
						validator: (rule, value, callback) => {
							return this.$u.test.chinese(value);
						},
						message: '姓名必须为中文',
						trigger: ['change','blur'],
					},
				],
				sex_lable: [
					{
						required: true,
						message: '请选择性别',
						trigger: ['change','blur'],
					},
				],
				id_card: [
					{
						validator: (rule, value, callback) => {
							return this.$u.test.idCard(value);
						},
						message: '身份证号格式不正确',
						trigger: ['change','blur'],
					}
				],
				living: [
					{
						required: true,
						message: '请填写现居住地址',
						trigger: ['change','blur'],
					},
				],
				domicile: [
					{
						required: true,
						message: '请填写户籍所在地',
						trigger: ['change','blur'],
					},
				],
				employer: [
					{
						required: true,
						message: '请填写工作单位',
						trigger: ['change','blur'],
					},
				],
				buy_way: [
					{
						required: true,
						message: '请填写出发地、途径地及时间',
						trigger: ['change','blur'],
					},
				],
				arrival_time: [
					{
						required: true,
						message: '请选择到达时间',
						trigger: ['change','blur'],
					},
				],
				departure: [
					{
						required: true,
						message: '请填写出发地',
						trigger: ['change','blur'],
					},
				],
				check_time: [
					{
						required: true,
						message: '请选择最后一次核酸检测时间',
						trigger: ['change','blur'],
					},
				],
				community_lable: [
					{
						required: true,
						message: '请选择居住地所在社区',
						trigger: ['change','blur'],
					},
				],
				mobile: [
					{
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码格式不正确',
						trigger: ['change','blur'],
					},
				],
				image1: [
					{
						required: true,
						message: '请上传健康码',
						trigger: ['change','blur'],
					},
				],
				image2: [
					{
						required: true,
						message: '请上传行程码',
						trigger: ['change','blur'],
					},
				],
				code: [
					{
						validator: (rule, value, callback) => {
							return this.$u.test.code(value, 6);
						},
						message: '请填写6位验证码',
						trigger: ['change','blur'],
					},
				],
			},
		};
	},
	onLoad() {
		
	},
	computed: {
		domicile_area_code: {
			get(){
				if (!this.domicile_code.province.length) {
					return []
				}
				return [this.domicile_code.province, this.domicile_code.city, this.domicile_code.area]
			},
			set(newValue){
				const arr = newValue.split('-')
				this.domicile_code.province = arr[0]
				this.domicile_code.city = arr[1]
				this.domicile_code.area = arr[2]
			}
		},
		departure_area_code: {
			get(){
				if (!this.departure_code.province.length) {
					return []
				}
				return [this.departure_code.province, this.departure_code.city, this.departure_code.area]
			},
			set(newValue){
				const arr = newValue.split('-')
				this.departure_code.province = arr[0]
				this.departure_code.city = arr[1]
				this.departure_code.area = arr[2]
			}
		}
	},
	onReady() {
		this.getCommunityFunc()
		this.$refs.uForm.setRules(this.rules)
		this.upload_lists1 = this.$refs.uUpload1.lists
		this.upload_lists2 = this.$refs.uUpload2.lists
	},
	methods: {
		submit() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					console.log('验证成功');
					this.$u.api.saveForm1(this.model).then(res => {
						console.log(res);
						if (res.code == 1) {
							this.$refs.uToast.show({
								title: '提交成功',
								type: 'success',
								url: 'pages/index/index',
							})
							return;
						}
						this.$refs.uToast.show({
							title: '提交失败：' + res.msg,
							type: 'default',
						})
					})
				} else {
					console.log('验证失败');
				}
			});
		},
		// 点击actionSheet回调
		actionSheetCallback(index) {
			uni.hideKeyboard();
			this.model.sex_lable = this.actionSheetList[index].text;
			this.model.sex = index;
		},
		// 户籍所在地回调
		domicileConfirm(e) {
			this.model.domicile = e.province.label + '-' + e.city.label + '-' + e.area.label
			// 记忆本次选择
			this.domicile_area_code = e.province.value + '-' + e.city.value + '-' + e.area.value
		},
		// 到沙时间回调
		arrivalTimeConfirm(e) {
			this.model.arrival_time = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute
		},
		// 出发地回调
		departureConfirm(e) {
			this.model.departure = e.province.label + '-' + e.city.label + '-' + e.area.label
			// 记忆本次选择
			this.departure_area_code = e.province.value + '-' + e.city.value + '-' + e.area.value
		},
		// 最近一次核酸检测时间回调
		checkTimeConfirm(e) {
			this.model.check_time = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute
		},
		// 最近一次核酸检测结果回调
		radioChange(index) {
			this.model.check_result = index
		},
		// 选择社区回调
		selectConfirm(e) {
			this.model.community = '';
			e.map((val, index) => {
				console.log(index)
				this.model.community_lable = val.label;
				this.model.community = val.value;
				this.selectDefault = [val.extra ?? 0]
			})
		},
		// 验证码倒计时改变
		codeChange(text) {
			this.codeTips = text;
		},
		// 获取验证码
		async getCode() {
			if(this.$refs.uCode.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码',
					mask: true
				})
				
				const {
					code,
					msg
				} = await this.$u.api.sendSms({
					mobile: this.model.mobile,
				});
				uni.hideLoading();
				if (code == 1) {
					this.$refs.uCode.start();
				}
				this.$u.toast(msg);
			} else {
				this.$u.toast('倒计时结束后再发送');
			}
		},
		async getCommunityFunc(){
			const {
				code,
				data
			} = await this.$u.api.getCommunityList()
			if (code == 1) {
				this.selectList = data
			} else {
				this.$refs.uToast.show({
					title: msg,
					type: 'error',
				})
			}
		},
		//健康码上传成功事件
		uploadChange1(data, index, lists, name){
			const res = JSON.parse(data.data)
			if (res.code == 1) {
				this.model.image1 = res.data.url
				this.$refs.uToast.show({
					title: '上传成功',
					type: 'success',
				})
				return;
			}
			this.$refs.uToast.show({
				title: '上传失败：' + res.msg,
				type: 'error',
			})
		},
		//行程码上传成功事件
		uploadChange2(data, index, lists, name){
			const res = JSON.parse(data.data)
			if (res.code == 1) {
				this.model.image2 = res.data.url
				this.$refs.uToast.show({
					title: '上传成功',
					type: 'success',
				})
				return;
			}
			this.$refs.uToast.show({
				title: '上传失败：' + res.msg,
				type: 'error',
			})
		}
	}
};
</script>

<style scoped lang="scss">
.wrap {
	padding: 30rpx;
}

.agreement {
	display: flex;
	align-items: center;
	margin: 40rpx 0;

	.agreement-text {
		padding-left: 8rpx;
		color: $u-tips-color;
	}
}
</style>
